<?php
namespace Admin\Controller;

use Admin\Controller\AdminController;
use Admin\Api\UserPictureApi;
use Common\Api\GamePrizeApi;
use Common\Api\GameApi;

class GamePrizeController extends AdminController{
    protected function _initialize() {
        parent::_initialize();
    }

    /**
     * show prize list by game id : the default page
     * @param  [int]    the game id
     * @return [html]
     */
    public function index($gid){
        $game = $this->getGameById($gid);
        $this->assignTitle($game['name'].'-奖池管理');
        $this->assign('game',$game);
        $prizes = $this->getPrizesByGameId($gid);
        $this->assign('list',$prizes);
        $this->assign('cats',$this->getPrizeCats());
        $this->display();
    }

    /**
     * delete GamePrize by it's id
     * @param  [int]    id
     * @return [type]
     */
    public function delete($pid){
        $prize = $this->getPrizeById($pid);
        $url = U('GamePrize/index', array('gid'=>$prize['game_id']));
        $r = apiCall(GamePrizeApi::DEL,array(array('id'=>$pid)));
        if($r['status'] && $r['info']){
            $this->success(L('RESULT_SUCCESS'),$url);
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * update GamePrize by it's id
     * @param  [int]    id
     * @return [type]
     */
    public function edit($pid){
        $prize = $this->getPrizeById($pid);
        $gid = $prize['game_id'];
        $game  = $this->getGameById($gid);
        $this->assignTitle($game['name'].' - 奖品编辑');

        if(IS_POST){
            $map = I('post.');
            $map['game_id']      = $gid;
            // dump($map);exit;
            $r = apiCall(GamePrizeApi::SAVEBYID,array($pid,$map));
            if ($r['status']) {
                $this->success(L('RESULT_SUCCESS'),U('GamePrize/index', array('gid'=>$gid)));
            }else{
                $this->error('奖品编辑失败');
            }
        }

        // $path = apiCall(UserPictureApi::GET_INFO,array(array('id'=>$prize['prize_icon'])));
        // $prize['imgpath'] = $path['info']['path'];
        $this->assign('cats',$this->getPrizeCats());
        $this->assign('game',$game);
        $this->assign('prize',$prize);
        $this->display();
    }

    /**
     * post: add the post GamePrize
     * get : show the add page
     */
    public function add($gid){
        $game = $this->getGameById($gid);
        $this ->assignTitle($game['name'].'- 奖品添加');
        $this ->assign('game',$game);
        if(IS_POST){
            $map            = I('post.');
            $map['game_id'] = $gid;
            $r              = apiCall(GamePrizeApi::ADD,array($map));
            if ($r['status']) {
                $this->success(L('RESULT_SUCCESS'),U('GamePrize/index', array('gid'=>$gid)));
            }else{
                $this->error('奖品添加失败');
            }
        }
        $this->assign('cats',$this->getPrizeCats());
        $this->display();

    }

    protected function getPrizeCats(){
        return array(1=>'积分',2=>'购物券',3=>'商品',4=>'其他');
        // $cat = D('Datatree')->where(array('code'=>'PrizeCat'))->find();
        // return D('Datatree')->where(array('parentid'=>$cat['id']))->select();
    }
    protected function getGameById($gid){
        $r = apiCall(GameApi::FIND,array(array('id'=>$gid)));
        if(!$r || !$r['status']) $this->error('GameApi:find:error');
        // if(!$r['info']) $this->error('错误抽奖');
        return $r['info'];
    }
    protected function getPrizesByGameId($gid){
        $r = apiCall(GamePrizeApi::GET_PRIZE,array(array('game_id'=>$gid),'sort'));
        if(!$r || !$r['status']) $this->error('GamePrizeApi:getPrize:error');
        // if(!$r['info']) $this->error('错误抽奖');
        return $r['info'];
    }
    protected function getPrizeById($pid){
        $r = apiCall(GamePrizeApi::FIND,array(array('id'=>$pid)));
        if(!$r || !$r['status']) $this->error('GamePrizeApi:find:error');
        // if(!$r['info']) $this->error('错误奖品');
        return $r['info'];
    }
}